package presentation;

import java.util.List;
import org.hibernate.*;

import db.*;

public class Get16ImagesBean {
	private Session session = null;
	private List<Picture> pictures;
	private long page;
	
	public Get16ImagesBean() {
		 this.session = DbUtils.startSession();
		 this.pictures = DbUtils.get16Pictures(session, 1);
	}
	public Get16ImagesBean(int pageNumber, Long albumId) {
		this.session = DbUtils.startSession();
		if(albumId!=null){
			this.pictures = DbUtils.get16Pictures(session, pageNumber,albumId);
		}else{
			this.pictures = DbUtils.get16Pictures(session, pageNumber);
		}
	};
	
	public List<Picture> getPictures() {
		return pictures;
	}
	
	public long getAllPicNum(Long albumId) {
		 this.session = DbUtils.startSession();
		 long allNum;
		 if(albumId!=null){
			 allNum = DbUtils.getAllPicturesNumByAlbum(session, albumId);
		 }else{
			 allNum = DbUtils.getAllPicturesNum(session);
		 }
		 if(allNum%16!=0){
				page=(allNum/16)+1;
			}else{
				page=(allNum/16);
			}
		 return this.page;
		
	}
	public  List<Comment> getCommentsByPicture(long pictId) {
		 this.session = DbUtils.startSession();
		 List<Comment> comments=DbUtils.getCommentsByPicture(session, pictId);
		 Hibernate.initialize(comments) ;
		 return comments;
	}
	
	public void releaseResources() {
		if(this.session != null) {
			this.session.close();
		}
	}
	
	public  boolean isUserPicture(String userName, long pictureId) {
		 this.session = DbUtils.startSession();
		 boolean bool=DbUtils.isUserPicture(session, userName, pictureId);
		 return bool;
	}
	
	public  boolean isUserComment(long userId, long commentId) {
		 this.session = DbUtils.startSession();
		 boolean bool=DbUtils.isUserComment(session, userId, commentId);
		 return bool;
		 
	}
	
	public  List<Album> getUserAlbums(long userId) {
		 this.session = DbUtils.startSession();
		 List<Album> albums=DbUtils.getUserAlbums(session, userId);
		 return albums;
	}

	public  String getPicturePath(long id) {
		this.session = DbUtils.startSession();
		String path=  DbUtils.getPicturePath(session, id);
		return path;
	}
	
	public long getAllAlbumsNum(long userId) {
		 this.session = DbUtils.startSession();
		 List<Album> albums=DbUtils.getUserAlbums(session, userId);
		long allAlb=albums.size();
	if(allAlb<16){
			page=1;
	} else{
		 if(allAlb%16!=0){
				page=(allAlb/16)+1;
		 }else{
				page=(allAlb/16);
		 }
	}
		 return page;
		
	}
	
	public List<Album>getAllAlbums() {
		 this.session = DbUtils.startSession();
		List<Album> albums= DbUtils.getAllAlbums(session);
		 return albums;		
	}
	
	public List<Picture>getAllPicturesByAlbum( long albumId) {
		 this.session = DbUtils.startSession();
		List<Picture> pictures= DbUtils.getAllPicturesByAlbum(session, albumId);
		 return pictures;	
	}
}

